package cn.com.hatech.blog.module.admin.setting.service.impl;

import cn.com.hatech.blog.module.admin.setting.entity.Setting;
import cn.com.hatech.blog.module.admin.setting.mapper.SettingMapper;
import cn.com.hatech.blog.module.admin.setting.service.IOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.module.admin.service.impl
 * @className: OptionServiceImpl
 * @author: xuruikai
 * @description: 系统配置业务层接口实现
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
@Service
@Slf4j
public class OptionServiceImpl implements IOptionService {

    @Resource
    private SettingMapper settingMapper;

    @Override
    public void insertOption(String name, String value) {
        log.debug("Enter insertOption method:name={},value={}", name, value);
        Setting option = new Setting();
        option.setName(name);
        option.setValue(value);
        if (settingMapper.selectList(null).size() == 0) {
            settingMapper.insert(option);
        } else {
            settingMapper.updateById(option);
        }
        log.debug("Exit insertOption method.");
    }

    @Override
    public void saveOptions(Map<String, String> options) {
        if (null != options && !options.isEmpty()) {
            options.forEach(this::insertOption);
        }
    }

    @Override
    public List<Setting> getOptions() {
        return settingMapper.selectList(null);
    }
}
